package li.com.springaidemo.timing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.PostConstruct;
import li.com.springaidemo.mapper.CronMapper;
import li.com.springaidemo.pojo.Cron;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.Instant;
import java.util.Date;
import java.util.List;

/**
 * @author Created by zhuzqc on 2023/1/30 15:58
 */
@Slf4j
@Component
@EnableScheduling
public class AddSchedule{

    @Autowired
    private CronMapper cronMapper;

    private List<Integer> ids;
    // 引入外部的业务数据对象
    public void putHiredTask(){
        ids = cronMapper.getOpenId();
        for(Integer id:ids) {
            Runnable runnable = getRunnable(id);
            //TODO: 将业务线程和定时触发器交由线程池工具管理：创建业务线程对象，并对属性赋初始化值（有参构造）
            SchedulerUtil.put(runnable, new Trigger() {
                @Override
                public Instant nextExecution(TriggerContext triggerContext) {
                    //获取定时触发器，这里可以获取页面的更新记录，实现定时间隔的动态调整
                    Instant nextExecTime = null;
                    //cron 表达式转换工具类

                    String cron = cronMapper.getCron(id);
                    try {
                        if (StringUtil.isNullOrEmpty(cron)) {
                            // 提示参数为空
                            log.info("trackScheduler定时器的 cron 参数为空！");
                            // 如果为空则表示不执行
                            cron = "0 0 5 31 2 ?";
                        }
                        log.info("-------定时任务执行中：" + cron + "--------");
                        CronTrigger cronTrigger = new CronTrigger(cron);
                        nextExecTime = cronTrigger.nextExecution(triggerContext);
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage());
                    }
                    return nextExecTime;
                }
            }, id);
        }
    }

    public void addHiredTask(List<Integer> ids){
        for(Integer id:ids) {
            Runnable runnable = getRunnable(id);
            //TODO: 将业务线程和定时触发器交由线程池工具管理：创建业务线程对象，并对属性赋初始化值（有参构造）
            SchedulerUtil.put(runnable, new Trigger() {
                @Override
                public Instant nextExecution(TriggerContext triggerContext) {
                    //获取定时触发器，这里可以获取页面的更新记录，实现定时间隔的动态调整
                    Instant nextExecTime = null;
                    //cron 表达式转换工具类

                    String cron = cronMapper.getCron(id);
                    try {
                        if (StringUtil.isNullOrEmpty(cron)) {
                            // 提示参数为空
                            log.info("trackScheduler定时器的 cron 参数为空！");
                            // 如果为空则表示不执行
                            cron = "0 0 5 31 2 ?";
                        }
                        log.info("-------定时任务执行中：" + cron + "--------");
                        CronTrigger cronTrigger = new CronTrigger(cron);
                        nextExecTime = cronTrigger.nextExecution(triggerContext);
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage());
                    }
                    return nextExecTime;
                }
            }, id);
        }
    }

    public Runnable getRunnable(Integer id){
        switch (id){
            case 1:{
                return new Runnable() {
                    @Override
                    public void run() {
                        OssTiming.timingDeleteImg();
                    }
                };
            }
            case 2:{
                return new Runnable() {
                    @Override
                    public void run() {
                        OssTiming.test1();
                    }
                };
            }
            case 3:{
                return new Runnable() {
                    @Override
                    public void run() {
                        OssTiming.test2();
                    }
                };
            }
        }
        return null;
    }
}

